package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private GoodsTypeService goodsTypeDaoService;

    @Autowired
    private SaleListGoodsService saleListGoodsService;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public Page<Goods> getListInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        PageHelper.startPage(page,rows);
        Page<Goods> pages = goodsDao.getListInventory(codeOrName,goodsTypeId);
        return pages;
    }

    @Override
    public Page<Goods> getList(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        PageHelper.startPage(page,rows);
        Page<Goods> page1 = goodsDao.getList(goodsName,goodsTypeId);
        return page1;
    }

    @Override
    public void save(Goods goods) {
        goodsDao.save(goods);
    }

    @Override
    public void update(Goods goods, Integer goodsId) {
        goodsDao.update(goods,goodsId);
    }

    @Override
    public int delete(Integer goodsId) {
        Goods goods = goodsDao.getStatus(goodsId);
        if (goods.getState() == 0){
            return goodsDao.delete(goodsId);
        }
        return 0;
    }

    @Override
    public Page<Goods> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        PageHelper.startPage(page,rows);
        Page<Goods> goods = goodsDao.getNoInventoryQuantity(nameOrCode);
        return goods;
    }

    @Override
    public Page<Goods> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        PageHelper.startPage(page,rows);
        Page<Goods> goods = goodsDao.getHasInventoryQuantity(nameOrCode);
        return goods;
    }

    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        goodsDao.saveStock(goodsId,inventoryQuantity,purchasingPrice);
    }

    @Override
    public int deleteStock(Integer goodsId) {
        Goods goods = goodsDao.getStatus(goodsId);
        if (goods.getState() == 0){
            return goodsDao.deleteStock(goodsId);
        }
        return 0;
    }

    @Override
    public List<Goods>  listAlarm() {
        List<Goods> goods = goodsDao.listAlarm();
        return goods;
    }


}
